package com.ebelter.myehc.adapters.historys;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ebelter.btcomlib.bases.activity.BaseActivity;
import com.ebelter.btcomlib.models.beans.ScaleUser;
import com.ebelter.btcomlib.models.bluetooth.products.three2one.ThreeOneManager;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.myehc.R;
import com.ebelter.myehc.models.http.response.GetUA;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class H_UA_A extends BaseAdapter {

    private Context mContext;

    private List<GetUA.ResultDataBean.DataListBean> datas;

    public H_UA_A(Context mContext) {
        this.mContext = mContext;
        datas=new ArrayList<>();
    }


    public void addData(List<GetUA.ResultDataBean.DataListBean> data){
        datas.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas!=null?datas.size():0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        VH vh;
        if (convertView==null){
            convertView=View.inflate(mContext, R.layout.item_xt_his,null);
            vh=new VH();
            vh.topLine=convertView.findViewById(R.id.item_xt_top_h_line);
            vh.buttomLine=convertView.findViewById(R.id.item_xt_buttom_h_line);
            vh.item_xt_time_tv=convertView.findViewById(R.id.item_xt_time_tv);
            vh.item_xt_date_tv=convertView.findViewById(R.id.item_xt_date_tv);
            vh.item_xt_item_tv=convertView.findViewById(R.id.item_xt_item_tv);
            vh.item_xt_item_tv.setText(BaseActivity.getString_(R.string.niaosuan)+"（umol/L）");
            vh.item_xt_value_tv=convertView.findViewById(R.id.item_xt_value_tv);
            convertView.setTag(vh);
        }else {
            vh= (VH) convertView.getTag();
        }




        Date date = TimeUtils.parseFormatter1Time(datas.get(position).time);
        String shiFen = TimeUtils.formatTime4(date.getTime());
        //String yueRi = TimeUtils.formatTime11(date.getTime());
        String yueRi = TimeUtils.timeFatmat(datas.get(position).time,1);
        vh.item_xt_time_tv.setText(shiFen);//时-分
        vh.item_xt_date_tv.setText(yueRi);//月-日

        float uricacid= NumUtils.string2Float(datas.get(position).uricacid);


        int age = ScaleUser.getUser().getAge();
        int sex = ScaleUser.getUser().getSex();//0男1女


        int result = ThreeOneManager.computerUAHResult(sex,age, (int) uricacid);//0正常 1偏高


        if (result==0){
            vh.item_xt_value_tv.setTextColor(mContext.getResources().getColor(R.color.textColor3));
        }else {
            vh.item_xt_value_tv.setTextColor(mContext.getResources().getColor(R.color.textColor5));
        }
        vh.item_xt_value_tv.setText((int) uricacid+"");

        adapterButtomLineView(vh.buttomLine,position,yueRi);
        return convertView;
    }


    /**
     * @param buttomLine 底部横线控件
     * @param position 当前位置position
     * @param currentYueRi 当前月-日字符串
     */
    private void adapterButtomLineView(View buttomLine, int position, String currentYueRi) {

        try {
            ViewUtils.displayView(buttomLine);
            String nextYueRi = TimeUtils.timeFatmat(datas.get(position+1).time,1);
            if (TextUtils.equals(currentYueRi,nextYueRi)){
                //当前的月日日期跟下一个的月日日期是一样的 底部横线gone
                ViewUtils.goneView(buttomLine);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    static class VH{
        View topLine;
        View buttomLine;
        TextView item_xt_time_tv;
        TextView item_xt_date_tv;
        TextView item_xt_item_tv;
        TextView item_xt_value_tv;
     }
}
